package com.otitan.la.forest.industry.utils;

import org.apache.commons.lang.StringUtils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.util.*;

/**
 * @Description: 当前类作用描述
 * @Author: xiazw
 * @CreateDate: 2020/8/18 14:00
 */
public class DateUtil {
    public static final String[] MONTHS = {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"};
    public static final String[] MONTHS_2020 = {"08", "09", "10", "11", "12"};

    private static final TimeZone b = TimeZone.getTimeZone("Asia/Shanghai");
    public static final String YYYY_MM_DD_HH_MI_SS = "yyyy-MM-dd HH:mm:ss";
    public static final String YYYYMMDDHHMISS = "yyyyMMddHHmmss";
    public static final String YYYYMMDD = "yyyyMMdd";
    public static final String YYYY_MM_DD = "yyyy-MM-dd";
    public static final String YYYY_MM = "yyyy-MM";

    public static String getCurrDate(String var0) {
        return dateToString(new Date(), var0);
    }

    /**
     * Return a Timestamp for right now
     *
     * @return Timestamp for right now
     */
    public static Timestamp nowTimestamp() {
        return new Timestamp(System.currentTimeMillis());
    }

    /**
     * Return a Date for right now
     *
     * @return Date for right now
     */
    public static Date nowDate() {
        return new Date();
    }

    public static Timestamp toTimestamp(String dateTime) {
        Date newDate = toDate(dateTime);

        if (newDate != null) {
            return new Timestamp(newDate.getTime());
        } else {
            return null;
        }
    }

    public static Date toDate(String dateTime) {
        // dateTime must have one space between the date and time...
        String date = dateTime.substring(0, dateTime.indexOf(" "));
        String time = dateTime.substring(dateTime.indexOf(" ") + 1);

        return toDate(date, time);
    }

    public static Date strToDate(String dateTime, String style) {
        SimpleDateFormat format = new SimpleDateFormat(style);
        try {
            Date parse = format.parse(dateTime);
            return parse;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Date strToDate1(String dateTime, String style) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat(style);
        Date parse = format.parse(dateTime);
        return parse;
    }

    public static String dateToString(Date var0, String var1) {
        String var2 = "";
        SimpleDateFormat var3 = new SimpleDateFormat(var1);
        var3.setTimeZone(b);

        try {
            var2 = var3.format(var0);
        } catch (Exception var5) {
        }

        return var2;
    }


    public static Date toDate(String date, String time) {
        if (date == null || time == null) {
            return null;
        }
        String month;
        String day;
        String year;
        String hour;
        String minute;
        String second;

        int dateSlash1 = date.indexOf("/");
        int dateSlash2 = date.lastIndexOf("/");

        if (dateSlash1 <= 0 || dateSlash1 == dateSlash2) {
            return null;
        }
        int timeColon1 = time.indexOf(":");
        int timeColon2 = time.lastIndexOf(":");

        if (timeColon1 <= 0) {
            return null;
        }
        month = date.substring(0, dateSlash1);
        day = date.substring(dateSlash1 + 1, dateSlash2);
        year = date.substring(dateSlash2 + 1);
        hour = time.substring(0, timeColon1);

        if (timeColon1 == timeColon2) {
            minute = time.substring(timeColon1 + 1);
            second = "0";
        } else {
            minute = time.substring(timeColon1 + 1, timeColon2);
            second = time.substring(timeColon2 + 1);
        }

        return toDate(month, day, year, hour, minute, second);
    }

    public static Date toDate(String monthStr, String dayStr,
                              String yearStr, String hourStr, String minuteStr, String secondStr) {
        int month, day, year, hour, minute, second;

        try {
            month = Integer.parseInt(monthStr);
            day = Integer.parseInt(dayStr);
            year = Integer.parseInt(yearStr);
            hour = Integer.parseInt(hourStr);
            minute = Integer.parseInt(minuteStr);
            second = Integer.parseInt(secondStr);
        } catch (Exception e) {
            return null;
        }
        return toDate(month, day, year, hour, minute, second);
    }

    public static Date toDate(int month, int day, int year, int hour,
                              int minute, int second) {
        Calendar calendar = Calendar.getInstance();

        try {
            calendar.set(year, month - 1, day, hour, minute, second);
        } catch (Exception e) {
            return null;
        }
        return new Date(calendar.getTime().getTime());
    }

    /**
     * 日期相加
     *
     * @param date 日期
     * @param day  天数
     * @return 返回相加后的日期
     */
    public static Date addDate(Date date, int day) {
        try {
            Calendar c = Calendar.getInstance();
            c
                    .setTimeInMillis(getMillis(date) + ((long) day) * 24 * 3600
                            * 1000);
            return c.getTime();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 返回毫秒
     *
     * @param date 日期
     * @return 返回毫秒
     */
    public static long getMillis(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.getTimeInMillis();
    }

    /**
     * Makes a date String in the format MM/DD/YYYY from a Date
     *
     * @param date The Date
     * @return A date String in the format MM/DD/YYYY
     */
    public static String toDateString(Date date) {
        if (date == null) {
            return "";
        }
        Calendar calendar = Calendar.getInstance();

        calendar.setTime(date);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int year = calendar.get(Calendar.YEAR);
        String monthStr;
        String dayStr;
        String yearStr;

        if (month < 10) {
            monthStr = "0" + month;
        } else {
            monthStr = "" + month;
        }
        if (day < 10) {
            dayStr = "0" + day;
        } else {
            dayStr = "" + day;
        }
        yearStr = "" + year;
        return monthStr + "/" + dayStr + "/" + yearStr;
    }

    /**
     * Makes a time String in the format HH:MM:SS from a Date. If the seconds
     * are 0, then the output is in HH:MM.
     *
     * @param date The Date
     * @return A time String in the format HH:MM:SS or HH:MM
     */
    public static String toTimeString(Date date) {
        if (date == null) {
            return "";
        }
        Calendar calendar = Calendar.getInstance();

        calendar.setTime(date);
        return (toTimeString(calendar.get(Calendar.HOUR_OF_DAY), calendar
                .get(Calendar.MINUTE), calendar.get(Calendar.SECOND)));
    }

    /**
     * Makes a time String in the format HH:MM:SS from a separate ints for hour,
     * minute, and second. If the seconds are 0, then the output is in HH:MM.
     *
     * @param hour   The hour int
     * @param minute The minute int
     * @param second The second int
     * @return A time String in the format HH:MM:SS or HH:MM
     */
    public static String toTimeString(int hour, int minute, int second) {
        String hourStr;
        String minuteStr;
        String secondStr;

        if (hour < 10) {
            hourStr = "0" + hour;
        } else {
            hourStr = "" + hour;
        }
        if (minute < 10) {
            minuteStr = "0" + minute;
        } else {
            minuteStr = "" + minute;
        }
        if (second < 10) {
            secondStr = "0" + second;
        } else {
            secondStr = "" + second;
        }
        if (second == 0) {
            return hourStr + ":" + minuteStr;
        } else {
            return hourStr + ":" + minuteStr + ":" + secondStr;
        }
    }


    // 将数字0,1,2转换为0，一，二
    public static String convertNum1(String src) {
        // 个位数字
        String[][] map = {
                {"1", "一"},
                {"2", "二"},
                {"3", "三"},
                {"4", "四"},
                {"5", "五"},
                {"6", "六"},
                {"7", "七"},
                {"8", "八"},
                {"9", "九"},
        };

        for (int i = 0; i < map.length; i++) {
            src = src.replaceAll(map[i][0], map[i][1]);
        }
        return src;
    }

    // 将数字01,11,22转换为一，十一，二十二
    public static String convertNum2(String src) {

        // 个位数字
        Map unitMap = new HashMap();
        unitMap.put("1", "一");
        unitMap.put("2", "二");
        unitMap.put("3", "三");
        unitMap.put("4", "四");
        unitMap.put("5", "五");
        unitMap.put("6", "六");
        unitMap.put("7", "七");
        unitMap.put("8", "八");
        unitMap.put("9", "九");

        //十位数字
        Map tenMap = new HashMap();
        tenMap.put("10", "十");
        tenMap.put("20", "二十");
        tenMap.put("30", "三十");

        String s1 = src.substring(0, 1);
        String s2 = src.substring(1, 2);

        if (s1.compareTo("0") > 0) { // 10,11,20,21
            if (s2.compareTo("0") == 0) { // 10,20
                src = (String) tenMap.get(s1 + s2);
            } else { // 11,21
                if (s1.compareTo("1") == 0) { // 11,12,13
                    src = "十" + (String) unitMap.get(s2);
                } else {
                    // 21,22,31
                    src = (String) unitMap.get(s1) + "十" + (String) unitMap.get(s2);
                }
            }
        } else { // 01,02,03
            src = (String) unitMap.get(s2);
        }

        return src;
    }


    /**
     * 日期相减 秒
     *
     * @param date  日期
     * @param date1 日期
     * @return 返回相减后的秒
     */
    public static int getDiffSecend(Date date, Date date1) {
        try {
            return (int) ((date.getTime() - date1.getTime()) / (1000));
        } catch (Exception e) {
            return 0;
        }
    }

    public static Date getDate(String time, String style) {
        try {
            SimpleDateFormat format = new SimpleDateFormat(style);
            return format.parse(time);
        } catch (Exception e) {
            return null;
        }
    }

    public static String formatDate(Date date, String style) {
        try {
            SimpleDateFormat format = new SimpleDateFormat(style);
            return format.format(date);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 格式化日期
     *
     * @param date1     日期
     * @param formatStr 格式
     * @return 返回字符型日期
     */
    public static String parseDate(Date date1, String formatStr) {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat(formatStr);
            return formatter.format(date1);
        } catch (Exception e) {
            return null;
        }
    }

    public static long getDatePoor(Date endDate, Date nowDate, String str) {

        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        if ("day".equals(str)) {
            return day;
        } else if ("hour".equals(str)) {
            return hour;
        } else if ("min".equals(str)) {
            return min;
        }
        return day;
    }

    /**
     * 获取当前时间 string
     *
     * @param []
     * @return java.lang.String
     * @throws
     * @author jiachangsen
     * @date 2020/08/12 15:30
     */
    public static String getNowDateStr() {
        return dateToString(new Date(), YYYYMMDDHHMISS);
    }

    /**
     * 获取最近一年的月份日期
     *
     * @param []
     * @return java.util.List<java.lang.String>
     * @throws
     * @author jiachangsen
     * @date 2020/09/28 10:32
     */
    public static List<String> getLastYear() {
        List<String> list = new ArrayList<>();
        Calendar c = Calendar.getInstance();
        for (int i = 0; i < 12; i++) {
            list.add(dateToString(c.getTime(), YYYY_MM));
            c.add(Calendar.MONTH, -1);
        }
        Collections.reverse(list);
        return list;
    }

    /**
     * 获取最近十天的日期
     *
     * @param []
     * @return java.util.List<java.lang.String>
     * @throws
     * @author jiachangsen
     * @date 2020/09/28 10:32
     */
    public static List<String> getLastTenDay() {
        List<String> list = new ArrayList<>();
        Calendar c = Calendar.getInstance();
        for (int i = 0; i < 7; i++) {
            list.add(dateToString(c.getTime(), YYYY_MM_DD));
            c.add(Calendar.DAY_OF_YEAR, -1);
        }
        Collections.reverse(list);
        return list;
    }

    /**
     * 获取最近一年的月份 开始日期和结束日期
     *
     * @param []
     * @return java.util.List<java.lang.String>
     * @throws
     * @author jiachangsen
     * @date 2020/09/28 10:32
     */
    public static Map<String, Date> getLastYearStartMonthAadEndMonth() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 1);
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.add(Calendar.DAY_OF_YEAR, -1);
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        Map<String, Date> resMap = new HashMap<>();
        resMap.put("end", c.getTime());
        c.add(Calendar.MONTH, -11);
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        resMap.put("start", c.getTime());
        return resMap;
    }

    /**
     * 获取最近十天的月份 开始日期和结束日期
     *
     * @param []
     * @return java.util.List<java.lang.String>
     * @throws
     * @author jiachangsen
     * @date 2020/09/28 10:32
     */
    public static Map<String, Date> getLastTenDayStartMonthAadEndMonth() {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        Map<String, Date> resMap = new HashMap<>();
        resMap.put("end", c.getTime());
        c.add(Calendar.DAY_OF_YEAR, -6);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        resMap.put("start", c.getTime());
        return resMap;
    }

    /**
     * 获取今年 开始日期和结束日期
     *
     * @param []
     * @return java.util.List<java.lang.String>
     * @throws
     * @author jiachangsen
     * @date 2020/09/28 10:32
     */
    public static Map<String, Date> getcurrentYearStartDateAadEndDate() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 1);
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.add(Calendar.DAY_OF_YEAR, -1);
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        Map<String, Date> resMap = new HashMap<>();
        resMap.put("end", c.getTime());
        c.set(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        resMap.put("start", c.getTime());
        return resMap;
    }

    /**
     * 获取今年的月份
     *
     * @param []
     * @return java.util.List<java.lang.String>
     * @throws
     * @author jiachangsen
     * @date 2020/09/28 10:32
     */
    public static List<String> getcurrentYearMonth() {
        List<String> list = new ArrayList<>();
        Calendar now = Calendar.getInstance();
        Calendar oneMonth = Calendar.getInstance();
        oneMonth.set(Calendar.MONTH, 0);
        oneMonth.set(Calendar.DAY_OF_MONTH, 1);
        oneMonth.set(Calendar.HOUR_OF_DAY, 0);
        oneMonth.set(Calendar.MINUTE, 0);
        oneMonth.set(Calendar.SECOND, 0);
        while (oneMonth.getTime().getTime() < now.getTime().getTime()) {
            list.add(dateToString(oneMonth.getTime(), YYYY_MM));
            oneMonth.add(Calendar.MONTH, 1);
        }
        return list;
    }

    /**
     * 获取最近一个小时，2个小时。。。。
     *
     * @param
     * @return java.lang.String
     * @throws
     * @author jiachangsen
     * @date 2020/08/12 15:30
     */
    public static Map<String, String> getLastDate(String dateTime) {
        if (StringUtils.isEmpty(dateTime)) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        String startDate = null;
        String endDate = parseDate(new Date(), "yyyy-MM-dd HH:mm:ss");
        Map<String, String> condition = new HashMap<>();
        switch (dateTime) {
            case "half_an_hour":
                calendar.add(Calendar.MINUTE, -5);
                startDate = parseDate(calendar.getTime(), "yyyy-MM-dd HH:mm:ss");
                break;
            case "an_hour":
                calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - 1);
                startDate = parseDate(calendar.getTime(), "yyyy-MM-dd HH:mm:ss");
                break;
            case "two_hours":
                calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - 2);
                startDate = parseDate(calendar.getTime(), "yyyy-MM-dd HH:mm:ss");
                break;
            case "three_hours":
                calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - 3);
                startDate = parseDate(calendar.getTime(), "yyyy-MM-dd HH:mm:ss");
                break;
            case "four_hours":
                calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - 4);
                startDate = parseDate(calendar.getTime(), "yyyy-MM-dd HH:mm:ss");
                break;
            case "day":
                String date = parseDate(new Date(), "yyyy-MM-dd");
                startDate = date + " 00:00:00";
                endDate = date + " 23:59:59";
                break;
            case "week":
                calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
                calendar.set(Calendar.HOUR_OF_DAY, 24);
                endDate = parseDate(calendar.getTime(), "yyyy-MM-dd HH:mm:ss");
                // condition.put("endDate",calendar.getTime());
                calendar.set(Calendar.HOUR_OF_DAY, -168);
                startDate = parseDate(calendar.getTime(), "yyyy-MM-dd HH:mm:ss");
                // condition.put("startDate",calendar.getTime());
                break;
            case "month":
                calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
                calendar.set(Calendar.HOUR_OF_DAY, 24);
                //condition.put("endDate",calendar.getTime());
                endDate = parseDate(calendar.getTime(), "yyyy-MM-dd HH:mm:ss");
                calendar.set(Calendar.HOUR_OF_DAY, -720);
                startDate = parseDate(calendar.getTime(), "yyyy-MM-dd HH:mm:ss");
                // condition.put("startDate",calendar.getTime());
                break;
            default:

        }
        condition.put("startDate", startDate);
        condition.put("endDate", endDate);
        return condition;
    }


    public static String getBeginTime(int year, int month) {
        YearMonth yearMonth = YearMonth.of(year, month);
        LocalDate localDate = yearMonth.atDay(1);
        LocalDateTime startOfDay = localDate.atStartOfDay();
        ZonedDateTime zonedDateTime = startOfDay.atZone(ZoneId.of("Asia/Shanghai"));

        return parseDate(Date.from(zonedDateTime.toInstant()), "yyyy-MM-dd HH:mm:ss");
    }

    public static String getEndTime(int year, int month) {
        YearMonth yearMonth = YearMonth.of(year, month);
        LocalDate endOfMonth = yearMonth.atEndOfMonth();
        LocalDateTime localDateTime = endOfMonth.atTime(23, 59, 59, 999);
        ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.of("Asia/Shanghai"));
        return parseDate(Date.from(zonedDateTime.toInstant()), "yyyy-MM-dd HH:mm:ss");
    }


    /**
     * string 转 Date
     *
     * @param [str]
     * @return java.lang.Integer
     * @throws
     * @author jiachangsen
     * @date 2020/08/12 14:00
     */
    public static Date cellStrToDate(String str) {
        if (str == null || "".equals(str)) {
            return null;
        }

        Date date = null;
        try {
            date = DateUtil.strToDate1(str, DateUtil.YYYY_MM_DD);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;

    }

}
